<?php  defined('BASEPATH') OR exit('No direct script access allowed');

class Hotels extends Admin_Controller {

	private $_validation_rules = array(
		array(
				'field'	=> 'available',
				'label'	=> 'lang:hotels.available',
				'rules'	=> 'is_natural'
		),
		array(
				'field'	=> 'rating',
				'label'	=> 'lang:hotels.rating',
				'rules'	=> 'numeric'
		),
		array(
				'field'	=> 'recommended',
				'label'	=> 'lang:hotels.recommended',
				'rules'	=> 'is_natural'
		),
		array(
				'field'	=> 'tel',
				'label'	=> 'lang:hotels.tel',
				'rules'	=> 'optional|numeric'
		),
		array(
				'field'	=> 'credits',
				'label'	=> 'lang:hotels.credits',
				'rules'	=> 'optional|integer|max_length[5]'
		),
		array(
				'field'	=> 'price',
				'label'	=> 'lang:hotels.price',
				'rules'	=> 'required|integer|max_length[5]'
		),
		array(
				'field'	=> 'discountprice',
				'label'	=> 'lang:hotels.discountprice',
				'rules'	=> 'optional|integer|max_length[5]'
		),
		array(
				'field'	=> 'checkin_time',
				'label'	=> 'lang:hotels.checkin_time',
				'rules'	=> 'required'
		),
		array(
				'field'	=> 'checkout_time',
				'label'	=> 'lang:hotels.checkout_time',
				'rules'	=> 'required'
		),
		array(
				'field'	=> 'city_id',
				'label'	=> 'lang:hotels.city_id',
				'rules'	=> 'required|is_natural_no_zero'
		),
		array(
				'field'	=> 'address',
				'label'	=> 'lang:hotels.address',
				'rules'	=> 'max_length[255]'
		),
		array(
				'field'	=> 'latitude',
				'label'	=> 'lang:hotels.latitude',
				'rules'	=> 'max_length[30]'
		),
		array(
				'field'	=> 'longitude',
				'label'	=> 'lang:hotels.longitude',
				'rules'	=> 'max_length[30]'
		),
		array(
				'field'	=> 'image',
				'label'	=> 'lang:hotels.image',
				'rules'	=> 'optional|numeric'
		),
		array(
				'field'	=> 'sort',
				'label'	=> 'lang:hotels.sort',
				'rules'	=> 'numeric'
		)
	);
	public function __construct()
	{
		parent::__construct();
		$this->lang->load('hotels/hotels');
		$this->load->model('hotels/hotels_model', 'hotels_m');
		
		$this->load->library('form_validation');
		$supported_languages = $this->config->item('supported_languages');
		
		foreach($supported_languages as $language => $lang_content){
			if(CURRENT_LANGUAGE == $language){
				$rules = 'required';
			}else{
				$rules = 'optional';
			}
			$this->_validation_rules[] = array(
					'field'	=> 'name_'. $language,
					'label'	=> 'lang:hotels.name_label',
					'rules'	=> $rules. '|max_length[80]'
			);
			$this->_validation_rules[] = array(
					'field'	=> 'introduction_'. $language,
					'label'	=> 'lang:hotels.introduction_label',
					'rules'	=> 'max_length[255]'
			);
			$this->_validation_rules[] = array(
					'field'	=> 'description_'. $language,
					'label'	=> 'lang:hotels.description_label',
					'rules'	=> 'max_length[2000]'
			);
			$this->_validation_rules[] = array(
					'field'	=> 'catering_'. $language,
					'label'	=> 'lang:hotels.catering',
					'rules'	=> 'max_length[30]'
			);
			$this->_validation_rules[] = array(
					'field'	=> 'internet_'. $language,
					'label'	=> 'lang:hotels.internet',
					'rules'	=> 'max_length[30]'
			);
			$this->_validation_rules[] = array(
					'field'	=> 'price_specification_'. $language,
					'label'	=> 'lang:hotels.price_specification',
					'rules'	=> 'max_length[255]'
			);
		}
		$this->form_validation->set_rules($this->_validation_rules);
		
		$this->load->model('regions/regions_model', 'regions_m');
		array_unshift($this->shortcuts, array('name' => lang('menu_2_catalog_hotels'), 'link' => site_url('admin/catalog/hotels')),
				array('name' => lang('menu_2_catalog_create_hotel'), 'link' => site_url('admin/catalog/create_hotel')));
		$this->template->set_data(array(
				'cities' => $this->regions_m->get_lang_list(array('region_type' => 3), CURRENT_LANGUAGE),
				'shortcuts' => $this->shortcuts
		));
	}

	// ------------------------------------------------------------------------

	public function index()
	{
		$pagination = create_pagination('admin/catalog/hotels' , $this->hotels_m->count_lang_list(FALSE, CURRENT_LANGUAGE), 15, 4);
		$hotels = $this->hotels_m->get_lang_list(array('offset' =>$pagination['limit'][1], 'limit' => $pagination['limit'][0]), CURRENT_LANGUAGE);
		$this->template->load('hotels/list', array(
			'pagination' => $pagination,
			'hotels' => $hotels
		));
	}

	// ------------------------------------------------------------------------

	/**
	 * Checks if the element save process can be done.
	 *
	 * @returns		Boolean		True if the save can be done, false if not
	 *
	 */
	function _check_before_save()
	{
		$default_lang = Settings_lib::get('site_lang');
		$default_lang_name = $this->input->post('name_'.$default_lang);
	
		if ($default_lang_name == FALSE)
		{
			return FALSE;
		}
	
		return TRUE;
	}
	
	// ------------------------------------------------------------------------
	
	public function create()
	{
		if ($this->form_validation->run())
		{
			if ($this->_check_before_save() == TRUE)
			{
				list($data, $lang_data) = $this->_prepare_data($this->hotels_m->get_table());

				$hotel_id = $this->hotels_m->save($data, $lang_data);
				
				if($hotel_id){
					$message	= lang('hotels.create_success');
					$status		= 'success';
				}else{
					$message	= lang('hotels.create_error');
					$status		= 'error';
				}
				$this->session->set_flashdata($status, $message);
				redirect('admin/catalog/hotels');
			}
		}

		foreach ($this->_validation_rules as $rules)
		{
			$hotel->{$rules['field']} = set_value($rules['field']);
		}
		$this->data['hotel'] = $hotel;
		$this->template->set('site_title', lang('hotels.create_title'))->load('hotels/create', $this->data);
	}

	// ------------------------------------------------------------------------

	public function edit($id = 0)
	{
		$hotel = $this->hotels_m->get($id);
		
		if ( ! $hotel)
		{
			redirect('admin/catalog/hotels');
		}
		$hotel_lang = $this->hotels_m->get_lang(array('id' => $id));
		$hotel->langs = fetch_lang($hotel_lang);
		
		if ($this->form_validation->run())
		{
			if ($this->_check_before_save() == TRUE)
			{
				list($data, $lang_data) = $this->_prepare_data($this->hotels_m->get_table());
				
				$hotel_id = $this->hotels_m->save($data, $lang_data);
	
				if($hotel_id){
					$message	= lang('hotels.create_success');
					$status		= 'success';
				}else{
					$message	= lang('hotels.create_error');
					$status		= 'error';
				}
			}
			$this->session->set_flashdata($status, $message);
			redirect('admin/catalog/hotels');
		}
		$this->load->model('regions/regions_model', 'regions_m');
		$this->data['cities'] = $this->regions_m->get_lang_list(array('region_type' => 3), CURRENT_LANGUAGE);
		$this->data['hotel'] = $hotel;
// 		p($hotel);
		$this->template->set('site_title', sprintf(lang('hotels.edit_title'), def_lang($hotel, 'name')))->load('hotels/edit', $this->data);
	}

	// ------------------------------------------------------------------------

	public function delete($id = 0)
	{
		$ids = $id
			? is_array($id)
				? $id
				: array($id)
			: (array) $this->input->post('action_to');

		$total		= sizeof($ids);
		$deleted	= array();

		// Try do deletion
		foreach ($ids as $id)
		{
			// Get the row to use a value.. as title, name
			if ($hotel = $this->hotels_m->_get($id, CURRENT_LANGUAGE))
			{
				// Make deletion retrieving an status and store an value to display in the messages
				$deleted[($this->hotels_m->delete($id) ? 'success': 'error')][] = $hotel->name;
			}
		}

		// Set status messages
		foreach ($deleted as $status => &$values)
		{
			// Mass deletion
			if (($status_total = sizeof($values)) > 1)
			{
				$last_value		= array_pop($values);
				$first_values	= implode(', ', $values);

				// Success / Error message
				$values = sprintf(lang('hotels.delete_mass_' . $status), $status_total, $total, $first_values, $last_value);
			}

			// Single deletion
			else
			{
				// Success / Error messages
				$values = sprintf(lang('hotels.delete_' . $status), $values[0]);
			}
		}

		// He arrived here but it was not done nothing, certainly valid ids not were selected
		if ( ! $deleted)
		{
			$status		= 'error';
			$deleted	= array('error' => lang('hotels.no_select_error'));
		}
		else
		{
			$status = array_key_exists('error', $deleted) ? 'error': 'success';
		}

		foreach ($deleted as $status => $message)
		{
			$this->session->set_flashdata($status, $message);
		}

		redirect('admin/catalog/hotels');
	}

	// ------------------------------------------------------------------------

	public function action()
	{
		$action = strtolower($this->input->post('btnAction'));
		
		if ($action)
		{
			// Get the id('s)
			$id_array = $this->input->post('action_to');

			// Call the action we want to do
			if (method_exists($this, $action))
			{
				return $this->{$action}($id_array);
			}
		}

		redirect('admin/catalog/hotels');
	}
}
